﻿using System.Collections.Generic;
using HMIS.FrameWork;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Authority;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
	public class MenuRepositoryImpl : RepositoryNhbImpl<MenuNode>, IMenuRepository
	{

		public override void SaveOrUpdate(MenuNode entity)
		{
			if (IsFieldExist("TreeCode", entity.TreeCode, entity.Id, null))
				throw new ExistException("TreeCode");
			base.SaveOrUpdate(entity);
		}

		public override void Delete(string id)
		{
			var entity = Get(id);
			if (IsChildForNotLeaf(id))
				throw new ValidationException("所选项下面存在子项,无法删除!");

			base.Delete(id);
		}


		#region IMenuRepository 成员

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		public IList<MenuNode> GetListByUserId_Old(string userId)
		{
			var query = Session.CreateQuery(@"select distinct t 
												from MenuNode as t
												left join t.Roles as r
												left join r.Persons p
											   where p.Id = :UserId and r.IsDelete = 0 and p.IsDelete = 0 and p.State = :PersonState and r.State=:RoleState order by t.Index asc")
				 .SetString("UserId", userId)
				 .SetEnum("PersonState", PersonState.Normal)
				 .SetEnum("RoleState", RoleState.Normal);

			return query.List<MenuNode>();
		}

		public IList<MenuNode> GetListByUserId(string userId)
		{
			var query = Session.CreateQuery(@"select distinct t 
												from MenuNode as t
												left join t.Users u
											   where t.IsFunction = 0 and u.Id = :UserId and u.State = :PersonState order by t.Index asc")
				 .SetString("UserId", userId)
				 .SetEnum("PersonState", PersonState.Normal);

			return query.List<MenuNode>();
		}

		public IList<MenuNode> GetAllListByUserId(string userId)
		{
			var query = Session.CreateQuery(@"select distinct t 
												from MenuNode as t
												left join t.Users u
											   where u.Id = :UserId and u.State = :PersonState order by t.Index asc")
				 .SetString("UserId", userId)
				 .SetEnum("PersonState", PersonState.Normal);

			return query.List<MenuNode>();
		}

		public IList<MenuNode> GetListByRoleId(string roleId)
		{
			var query = Session.CreateQuery(@"select distinct t 
												from MenuNode as t
												left join t.Roles as r
											   where r.Id = :RoleId and r.IsDelete = 0 and r.State = :RoleState")
				 .SetString("RoleId", roleId)
				 .SetEnum("RoleState", RoleState.Normal);
			return query.List<MenuNode>();
		}

		public IList<MenuNode> GetUserFavoriteList(string userId)
		{
			var query = Session.CreateQuery(@"select distinct t 
												from MenuNode as t
												left join t.FavoritedUsers u
												left join u.Roles ur
											   where u.Id = :UserId and u.IsDelete = 0 and ur.IsDelete = 0 and u.State = :PersonState and ur.State = :RoleState
												 and exists (select m from MenuNode as m
															   left join m.Roles as mr
															  where mr.Id = ur.Id and t.Id = m.Id
																and mr.IsDelete = 0 and mr.State = :RoleState)")
				 .SetString("UserId", userId)
				 .SetEnum("PersonState", PersonState.Normal)
				 .SetEnum("RoleState", RoleState.Normal);
			return query.List<MenuNode>();
		}

		#endregion
		private bool IsChildForNotLeaf(string id)
		{
			var query = Session.CreateQuery(@"select count(*) from MenuNode as o where o.ParentId=:ParentId")
				.SetString("ParentId", id);
			return query.UniqueResult<long>() > 0;
		}

	}
}
